<template>
	<view class='page_cont'>
		<view class='top_wrap'>
			<view class='title'>身份验证</view>
			<view class='mobile'>{{phone}}</view>
		</view>
		<view class='box'>
			<input class='input' maxlength="6" @input="input_code" placeholder="请输入验证码" placeholder-style="color:rgba(66,71,82,0.5);"></input>
			<view class="btn" @click="send_code()" v-if="show_btn">发送验证码</view>
			<view class='text' v-else>重新发送({{time}})</view>
		</view>
		<view class='con_btn' :style="{background:code?'rgba(91,144,255,1)':'rgba(91,144,255,0.5)'}" @click="confirm_code()">确定</view>
	</view>
</template>

<script>
	import resume from "../../api/myResume.js"
	import home from "../../api/home.js"
	import util from "../../utils/util.js"
	export default {
		data() {
			return {
				mobile: '',
				show_btn: true,
				time: 59,
				code: '',
				phone: '',
				bingQueryList: {
					businessId: -1,
					businessName: '',
					principalPhone: '',
					businessLogo: '',
					isLogin: -1
				},
				principalPhone: '',
				business_Info:null
			}
		},
		onLoad(options) {
			var that = this
			uni.getStorage({
				key: 'business_Info',
				success: function(res) {
					that.principalPhone=JSON.parse(res.data).principalPhone
					that.business_Info=JSON.parse(res.data)
					let reg = /^(\d{3})\d*(\d{4})$/
					that.phone = that.principalPhone.replace(reg, '$1****$2')
				}
			})
		},
		methods: {
			// 验证码
			input_code(e) {
				this.code = e.detail.value;
			},
			// 发送验证码
			send_code() {
				this.show_btn = false;
				var timer = setInterval(() => {
					this.time--;
					if (this.time <= 0) {
						this.show_btn = true;
						this.time = 59;
						clearInterval(timer)
					}
				}, 1000)
				resume.send_message_to_rector({
					phone: this.principalPhone
				}).then(res => {
					if(res.data.code==='200'){
						util.page_show_toast('发送成功',1500)
					}else{
						util.page_show_toast('发送失败',1500)
					}
				})
			},
			// 用户绑定单位
			confirm_code() {
				resume.is_validate_corrgite({
					phone: this.business_Info.principalPhone,
					code: this.code
				}).then(res => {
					if (res.data.code === '200') {
						// 用户绑定
						resume.bind_business_company(this.business_Info).then(response=>{
							if(response.data.code==='200'){
								console.log(response.data);
								this.saveLoginStatus(response.data.data.id)
								util.page_show_toast('绑定成功',1500)
								uni.setStorageSync('identityNum', 2);
								setTimeout(()=>{
									uni.switchTab({
										url:'../mypage/mypage'
									})
								},1500)
							}
						})
					}else{
						util.page_show_toast('验证码有误',1500)
					}
				})
			},
			// 记录下次登录状态
			saveLoginStatus(id){
				resume.save_login_identity({id:id,identity:1}).then(res=>{
					console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss">
	.page_cont {
		padding: 0 48rpx;

		.top_wrap {
			padding: 108rpx 0 72rpx 0;

			.title {
				text-align: center;
				height: 56rpx;
				font-size: 40rpx;
				font-weight: 600;
				line-height: 56rpx;
				color: #3F4A58;
				margin-bottom: 20rpx;
			}

			.mobile {
				text-align: center;
				height: 84rpx;
				font-size: 60rpx;
				font-weight: 600;
				line-height: 84rpx;
				color: #3F4A58;
			}
		}

		.box {
			padding: 32rpx 32rpx 32rpx 44rpx;
			background: #F7F9FB;
			border-radius: 16rpx;
			display: flex;
			flex-direction: row;
			margin-bottom: 48rpx;

			.input {
				flex: 1;
				text-align: left;
				min-height: 36rpx;
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(66, 71, 82, 1);
			}

			.btn {
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: #5B90FF;
			}

			.text {
				height: 42rpx;
				font-size: 30rpx;
				font-weight: 400;
				line-height: 42rpx;
				color: rgba(91, 144, 255, 0.5);
			}
		}

		.con_btn {
			height: 104rpx;
			line-height: 104rpx;
			text-align: center;
			font-size: 36rpx;
			border-radius: 16rpx;
			font-weight: 600;
			color: #fff;
			letter-spacing: 4rpx;
		}
	}
</style>
